package engine.newUi
{
	import engine.control.loading.MultLoader;
	import engine.control.loading.NxLoading;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.RoleVo;
	import engine.logic.LogSendLogic;
	import engine.logic.tips.TipsManage;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	
	import newx.StageInfo;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.window.NxWindowEvent;
	import newx.utils.FilterUtil;

	public class VipLogic
	{
		public static var _self:VipLogic;
		public 	 var ui			:VipUI ;
		private var _role:RoleVo ; //角色
		private var _model:ModelLocator;//全局静态变量
		private var vipLevel:NxLoading ; 
		private var _indexOneCount:int = -1 ; //绑定等级
		private var _indexTwoCount:int = -1 ; //绑定当前经验
		private var _indexThreeCount:int = -1 ; //绑定下一级的经验
		private var _bingManage:BindManage;
		
		private var _nowVipExe:int ; 
		private var _alwaysVipExe:int ; 
		private var _nowLevel:int ; 
		private var array:Array ; 
		private var page:uint; 
		private var max:uint ; 
		
		private var arrStr:Array ; 
		private var backMc:MovieClip; 
		
		/*****Css_text_color*颜色配置*/
		private var textformat:TextFormat;
		/****/
		private var color_Obj:Object;
		private var color_Str:String;
		//单例--               yangchenguang
		public static function getInstence():VipLogic
		{
			if(_self==null)
			{
				_self= new VipLogic(); 
			}
			return _self; 
		}
		
		//初始化数据
		public function Init():void
		{
			arrStr=["VIPLv_1","VIPLv_2","VIPLv_3","VIPLv_4","VIPLv_5","VIPLv_6","VIPLv_7","VIPLv_8","VIPLv_9","VIPLv_10"]; 
			ui=VipUI.getInstence(); 
			_model=ModelLocator.getInstance() ; 
			_role=_model.scene.Role; 
			_bingManage=BindManage.getInstance() ; 
			ui.text.x+=20; 
			ui.text.y+=20 ; 
			color_Obj=StaticIni.getIniObj("Css_text_color") ; 
			color_Str = "#"+color_Obj["c13"].toString()
		
		}
		private function bind():void
		{
			if(_indexOneCount==-1)
			{
				_indexOneCount = _bingManage.BindProp(_model.scene.Role,"VIPLevel",this,"levelChange");
			}
			if(_indexTwoCount==-1)
			{
				_indexTwoCount = _bingManage.BindProp(_model.scene.Role,"VIPExp",this,"getVipNowExe");
			}
			if(_indexThreeCount==-1)
			{
				_indexThreeCount = _bingManage.BindProp(_model.scene.Role,"VIPNextExp",this,"getVipAlwaysExe");
			}
		}
		private function unbind():void
		{
			if(_indexOneCount!=-1)
			{
				_indexOneCount=_bingManage.UnbindProp(_model.scene.Role,"VIPLevel",_indexOneCount);
			}
			if(_indexTwoCount!=-1)
			{
				_indexTwoCount =_bingManage.UnbindProp(_model.scene.Role,"VIPExp",_indexTwoCount);
			}
			if(_indexThreeCount!=-1)
			{
				_indexThreeCount = _bingManage.UnbindProp(_model.scene.Role,"VIPNextExp",_indexThreeCount);
			}
		}
		//显示界面的时候你要申请的东西
		public function BeforeShow():void
		{
			var textformat:TextFormat= new TextFormat();
			ui.text.glowColor=0xffffff;
			ui.text_2.glowColor	=0xffffff;
			ui.label_1.glowColor=0xffffff;
//			ui.label_9.glowColor=0xffffff;
			ui.page.glowColor = 0xffffff ; 
			textformat.font="黑体";
			textformat.bold = true ;
			ui.label_1.setTextFormat(textformat);
//			ui.label_9.setTextFormat(textformat);
			ui.page.setTextFormat(textformat)
			ui.text_2.wordWrap=true;
//			if( !MultLoader.getInstance().hasSourceByUrl(StageInfo.swfResPath + "vip.swf") )
//			{
//				MultLoader.getInstance().load([StageInfo.swfResPath + "vip.swf"],true,createBack);
//			}
//			else
//			{
				createBack();
//			}
			
		}
		private function createBack():void
		{
			var mc : MovieClip =MultLoader.getInstance().getSourceByUrl(StageInfo.swfResPath + "vip.swf");
			ui.backmc.source=mc;
			page=1;
			addListence(); 
			ui.page.htmlText=StaticIni.getSwfLangStrVar("UIString_1000472",[page])
			ui.label_1.htmlText=StaticIni.getSwfLangStrVar("VIPTips_1",[page]);
			bind();
		}
		public function set  levelChange(value:int):void 
		{
			if(ui.text == null)return;
			ui.text.htmlText="";
			_nowLevel=value ; 
//			ui.label_9.htmlText = StaticIni.getSwfLangStrVar("VIPTips_13",[value]);
			if(value==0)
			{
				ui.text.htmlText="<font color='"+color_Str+"'>"+StaticIni.getSwfLangStr(arrStr[value])+"</font>"; 
				ui.vip1.gotoAndStop(1);
				ui.vip1.filters=[FilterUtil.GrayColorFilter];
				ui.text_2.htmlText =StaticIni.getSwfLangStr("VIPTips_0");
				
			}else 
			{
				
				page = value ; 
				
				if(value!=10)
				{
					
					ui.page.htmlText=StaticIni.getSwfLangStrVar("UIString_1000472",[page+1]);
					ui.label_1.htmlText=StaticIni.getSwfLangStrVar("VIPTips_1",[value+1]);
					ui.text.htmlText="<font color='"+color_Str+"'>"+StaticIni.getSwfLangStr(arrStr[value])+"</font>"; 
					ui.text_2.htmlText=StaticIni.getSwfLangStr(arrStr[value-1]);
				}else 
				{
					ui.page.htmlText=StaticIni.getSwfLangStrVar("UIString_1000472",[page]);
					ui.label_1.htmlText=StaticIni.getSwfLangStrVar("VIPTips_1",[value]);
					ui.text.htmlText="<font color='"+color_Str+"'>"+StaticIni.getSwfLangStr(arrStr[value-1])+"</font>"; 
					ui.text_2.htmlText=StaticIni.getSwfLangStr(arrStr[value-1]);
				}
				
				page+=1 ;
				ui.vip1.filters=[];
				ui.vip1.visible =true ; 
				ui.vip1.gotoAndStop(value+1);
			}
			
		}
		public function set getVipNowExe(value:int):void
		{
			_nowVipExe = value;
//			ui.vipprice.htmlText =_nowVipExe.toString()+"/"+_alwaysVipExe.toString();
			ui.vipprice.htmlText=StaticIni.getSwfLangStrVar("UIString_1000485",[_nowVipExe.toString(),_alwaysVipExe.toString()])
			
			ui.vipexe.bar= (_nowVipExe/_alwaysVipExe)*100; 
		}
		public  function set getVipAlwaysExe(value:int):void
		{
			_alwaysVipExe = value;
//			ui.vipprice.htmlText =_nowVipExe.toString()+"/"+_alwaysVipExe.toString();
			ui.vipprice.htmlText=StaticIni.getSwfLangStrVar("UIString_1000485",[_nowVipExe.toString(),_alwaysVipExe.toString()])
			ui.vipexe.bar= (_nowVipExe/_alwaysVipExe)*100; 
			
		}
		//关闭界面的时候你回收的东西
		public function AfterHide():void
		{
			
			ui.text.setFilters(null)
			ui.text_2.setFilters(null)
			ui.label_1.txtFilters=[];
//			ui.label_9.txtFilters=[];
			ui.page.txtFilters = [] ; 
			unbind(); 
			removeListence(); 
		}
		//监听事件
		private function addListence():void 
		{
			ui.leftbtn.addEventListener(MouseEvent.CLICK,lefttHandler);
			ui.rightbtn.addEventListener(MouseEvent.CLICK,rightHandler);
			ui.btn.addEventListener(MouseEvent.CLICK ,VipHandler);
			ui.vipexe.addEventListener( MouseEvent.MOUSE_OVER, onShowTips );
			ui.vipexe.addEventListener( MouseEvent.MOUSE_OUT, onHideTips ); 
			ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,closeHandler);
		}
		//移除监听
		private function removeListence():void
		{
			ui.btn.removeEventListener(MouseEvent.CLICK ,VipHandler);
			ui.vipexe.removeEventListener( MouseEvent.MOUSE_OVER, onShowTips );
			ui.vipexe.removeEventListener( MouseEvent.MOUSE_OUT, onHideTips ); 
		}
		//添加一个网页连接
		private function VipHandler(e:MouseEvent):void
		{
			_role.recharge();
			LogSendLogic.getInstance().btnClickNumSend(22802);
		}
		//鼠标组件上悬浮提示框
		private function onShowTips(evt:MouseEvent):void
		{
			if((_nowLevel+1>10))
			{
				var _tip:String =StaticIni.getSwfLangStr("VIPTips_3") ;  
				TipsManage.showTips( _tip );
			}else 
			{
				var tip:String =StaticIni.getSwfLangStrVar("VIPTips_2",[(_alwaysVipExe-_nowVipExe),(_nowLevel+1)]) ;  
				TipsManage.showTips( tip );
			}
			
			
		}
		//鼠标离开悬浮提示框
		private function onHideTips(evt:MouseEvent):void
		{
			TipsManage.hideTips( );
		}
		private function closeHandler(e:NxWindowEvent):void
		{
			ui.showOrHide();
		}
		private function allVipNews():void 
		{
			ui.text.htmlText="";
			for(var i:uint=(page-1)*5 ;i<page*5;i++)
			{
				if(i<array.length)
				{
					ui.text.htmlText +=array[i]  ; 
				}
			}
				
		}
		private function lefttHandler(e:MouseEvent):void
		{
			if(page==1)return ;
			if(page>=10)
			{
				page=10;
			}
			ui.text.htmlText="";
			page--;
			ui.text.htmlText="<font color='"+color_Str+"'>"+vipExplain(page)+"</font>";
			ui.page.htmlText=StaticIni.getSwfLangStrVar("UIString_1000472",[page]);
			ui.label_1.htmlText=StaticIni.getSwfLangStrVar("VIPTips_1",[page]);
		}
		private function rightHandler(e:MouseEvent):void
		{
			if(page>=10){
				page=10;
				return ; 
			}
			page++;
			ui.text.htmlText=""; 
			//allVipNews();
			ui.text.htmlText="<font color='"+color_Str+"'>"+vipExplain(page)+"</font>";
			ui.page.htmlText=StaticIni.getSwfLangStrVar("UIString_1000472",[page])
			ui.label_1.htmlText=StaticIni.getSwfLangStrVar("VIPTips_1",[page]);
		}
		private function vipNewsNum(array:Array):void
		{
			if(array.length%5==0)
			{
				max=uint(array.length/5);
			}else
			{
				max=uint((array.length/5)+1);
			}
		}
		//翻页读取的VP1 等级 说明
		private function vipExplain(page:uint):String
		{
			var str:String="" ; 
			str= StaticIni.getSwfLangStr(arrStr[page-1]);
			return str ; 
		}
	
	}
}